package com.soonfor.evaluate.presenter;
import android.app.Activity;
import android.content.Context;

import com.estronger.shopmanage.ui.othertask.bean.OtherTaskMainBean;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

import com.soonfor.evaluate.bean.EvaluateListBean;
import com.soonfor.evaluate.base.IEvalBasePresenter;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;
import com.soonfor.evaluate.activity.fragment.Evaluated_CustomersToMeFragment;
import com.soonfor.evaluate.activity.fragment.UnEvaluate_CustomersToMeFragment;

/**
 * 作者：DC-DingYG on 2018-10-17 15:20
 * 邮箱：dingyg012655@126.com
 */
public class Evaluate_CustomersToMePresenter implements IEvalBasePresenter, AsyncUtils.AsyncCallback {

    private Activity mActivity;
    private Evaluated_CustomersToMeFragment yFragment;
    private UnEvaluate_CustomersToMeFragment nFragment;


    public Evaluate_CustomersToMePresenter(Evaluated_CustomersToMeFragment yFragment, Activity mActivity) {
        this.yFragment = yFragment;
        this.mActivity = mActivity;
    }

    public Evaluate_CustomersToMePresenter(UnEvaluate_CustomersToMeFragment nFragment, Activity mActivity) {
        this.nFragment = nFragment;
        this.mActivity = mActivity;
    }

    /**
     * //获取客户对我的评价列表
     * @param taskatate 评价状态 0待评价 1已评价
     * @param pageNo
     */
    public void getEvaluatedList(Context mContext, int taskatate, int pageNo, int requestCode) {
       Request.Evaluate.getEvaluateList(mContext, 0, taskatate, pageNo, requestCode, this);
    }

    @Override
    public void success(int requestCode, String data) {
        final Gson gson = new Gson();
        switch (requestCode) {
            case Request.Evaluate.GET_EVALUATETOMELIST_Y:
                try {
                    JSONArray ja = new JSONArray(data);
                    List<EvaluateListBean> mLists = new ArrayList<>();
                    if(ja!=null && ja.length()>0){
                        for(int i=0; i<ja.length(); i++){
                            JSONObject ja_o = ja.getJSONObject(i);
                            EvaluateListBean mBean = gson.fromJson(ja_o.toString(), EvaluateListBean.class);
                            mLists.add(mBean);
                        }
                    }
                    yFragment.showYList(true, mLists, "数据为空");
                }catch (Exception e){
                    yFragment.showYList(false, null, "请求出错");
                }
                yFragment.closeLoadingDialog();
                break;
            case Request.Evaluate.GET_EVALUATETOMELIST_N:
                try {
                    JSONArray ja = new JSONArray(data);
                    List<EvaluateListBean> mLists = new ArrayList<>();
                    if(ja!=null && ja.length()>0){
                        for(int i=0; i<ja.length(); i++){
                            JSONObject ja_o = ja.getJSONObject(i);
                            EvaluateListBean mBean = gson.fromJson(ja_o.toString(), EvaluateListBean.class);
                            mLists.add(mBean);
                        }
                    }
                    nFragment.showNList(true,  mLists, "数据为空");
                }catch (Exception e){
                    nFragment.showNList(false,null, "请求出错");
                }
                nFragment.closeLoadingDialog();
                break;
        }

    }

    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        switch (requestCode) {
            case Request.Evaluate.GET_EVALUATETOMELIST_Y:
                yFragment.closeLoadingDialog();
                yFragment.showYList(false, null, msg);
               yFragment.closeLoadingDialog();
                break;
            case Request.Evaluate.GET_EVALUATETOMELIST_N:
                nFragment.showNList(false, null, msg);
                nFragment.closeLoadingDialog();
                break;
        }
    }

    /**
     * 转为其它任务对象
     */
    public OtherTaskMainBean.DataBean convertOtherTaskBean(EvaluateListBean goBean) {
        if (goBean != null) {
            OtherTaskMainBean.DataBean result = new OtherTaskMainBean.DataBean();
            result.setFplantaskid(goBean.getFplantaskid());
            result.setFprocstatedesc("已完成");
            result.setFplantasktype(goBean.getFappraiseoritype());
            return result;
        } else return null;
    }

}
